package com.bianmaba.log;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: hadoop-samples
 * @description:
 * @author: Chenjiabin
 * @create: 2019/6/11 15:57
 **/
public class TimestampToDate extends UDF {
    private final SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Text evaluate(Text input) {
        // 过滤
        if (input == null) {
            return null;
        }

        Text output = new Text();

        try {
            Integer inputDate = Integer.parseInt(input.toString());
            Date parseDate = new Date(inputDate);
            String outputDate = outputFormat.format(parseDate);
            output.set(outputDate);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return output;
    }

    public static void main(String[] args) {
        Text t = new TimestampToDate().evaluate(new Text("123456789"));
        System.out.println(t.toString());
    }
}
